package com.woniuxy.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

import java.nio.charset.StandardCharsets;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        //创建RedisTemplate对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //设置连接工厂
        template.setConnectionFactory(factory);
        //配置键的序列化器：使用字符串类型
        template.setKeySerializer(new StringRedisSerializer());
        //设置值的序列化器：使用fastjson的序列化器
       // template.setValueSerializer(new GenericFastJsonRedisSerializer());

        // 使用自定义的序列化器
        template.setValueSerializer(new CustomRedisSerializer());


        //设置hash类型的序列化器：分别配置字段和值
        template.setHashKeySerializer(new StringRedisSerializer());
      //  template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
        FastRedisSerializer fastRedisSerializer = new FastRedisSerializer();
        template.setHashValueSerializer(fastRedisSerializer);
        return template;
    }

    public class CustomRedisSerializer implements RedisSerializer<Object> {

        private final StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        private final GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();

        @Override
        public byte[] serialize(Object value)  {
            if (value instanceof String) {
                return stringRedisSerializer.serialize((String) value);
            } else {
                return fastJsonRedisSerializer.serialize(value);
            }
        }

        @Override
        public Object deserialize(byte[] bytes)  {
            if (bytes == null || bytes.length == 0) {
                return null;
            }

            // 尝试使用fastjson反序列化
            try {
                return fastJsonRedisSerializer.deserialize(bytes);
            } catch (Exception e) {
                // 反序列化失败，尝试解析为字符串
                return new String(bytes, StandardCharsets.UTF_8);
            }
        }
    }

    class FastRedisSerializer extends GenericFastJsonRedisSerializer {
        @Override
        public byte[] serialize(Object object) throws SerializationException {
            if (object == null) {
                return new byte[0];
            }
            //处理hash value 存储 无法执行自增  ERR hash value is not an integer
            if (object instanceof Number) {
                return JSON.toJSONBytes(object);
            }
            return super.serialize(object);
        }
    }



}
